graphql / express-graphql

Create a GraphQL HTTP server with Express.
MIT License
6.34k stars 538 forks source link

call graph query from the same server #729

Closed akelmj closed 2 years ago

akelmj commented 3 years ago

Hi,

i want to call graphql query from the same server , this is my code example:

` const PORT = 4000;

var app = express();

app.use( '/graphql', graphqlHTTP({ schema: schema, graphiql: true, }), );

app.get('/posts', function (req, res) {

let myquery = query{ posts{ id title } } ;

  let result = // graphql call here

res.send(result)

})

app.listen(PORT); `

is it possible?

thanks.

XavierLeTohic commented 3 years ago

Hi,

You can directly use graphql from graphql package without using an HTTP server, pass it the schema and a query to use it:

import { graphql } from 'graphql';
import mySchema from '_path_to_schema';

const queryString = `{
  user {
    id
    email
  }
}`;

const query = await graphql(mySchema, queryString);
akelmj commented 2 years ago

work , thanks.

gabbigum commented 2 years ago

Hi, @akelmj you can close the issue now. Thanks :)

akelmj commented 2 years ago

:)