I'm creating an app in react-native and GraphQL.
And following is the code In using for connection to graphql:
app.use('/graphql', graphqlHTTP({
schema: MySessionAwareGraphQLSchema,
graphiql: process.env.NODE_ENV === 'development',
}));
Can we not have everything in the app itself? Why do we need a server/endpoint?
CodePudding user response:
Assuming graphqlHTTP
in this example is express-graphql
, GraphQL doesn't need an endpoint, but express-graphql
does. Here's the example from 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
here is your schema. source
is the query you're trying to make. rootValue
is the resolvers object.
Generally speaking, though, GraphQL is usually used as an API into your data layer. If you're building an app that doesn't have an external data layer (read "an API"), you may not want to fuss with it.
If you do have an external data layer and you're just not using HTTP, that's fine. GraphQL doesn't require HTTP or any other protocol. That's just the method that most people use.