我正在用 react-native 和 GraphQL 創建一個應用程式。
以下是用于連接到graphql的代碼:
app.use('/graphql', graphqlHTTP({
schema: MySessionAwareGraphQLSchema,
graphiql: process.env.NODE_ENV === 'development',
}));
我們不能在應用程式本身中擁有一切嗎?為什么我們需要服務器/端點?
uj5u.com熱心網友回復:
假設graphqlHTTP在這個例子中是express-graphql,GraphQL 不需要端點,但express-graphql確實需要。這是來自graphql-js的示例。
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The rootValue provides a resolver function for each API endpoint
var rootValue = {
hello: () => {
return 'Hello world!';
},
};
// Run the GraphQL query '{ hello }' and print out the response
graphql({
schema,
source: '{ hello }',
rootValue
}).then((response) => {
console.log(response);
});
schema這是您的架構。source是您要進行的查詢。rootValue是決議器物件。
不過,一般來說,GraphQL通常用作資料層的 API。如果你正在構建一個沒有外部資料層的應用程式(閱讀“一個 API”),你可能不想對它大驚小怪。
如果您確實有一個外部資料層并且您只是不使用 HTTP,那很好。GraphQL 不需要 HTTP 或任何其他協議。這只是大多數人使用的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526316.html
上一篇:節點快速檢查mongo物件id是否存在于陣列中,如果不存在則更新
下一篇:單擊“計算”按鈕后出現錯誤
