Skip to content

express-graphql / graphqlHTTP implementation exampleย #51

@bouviervj

Description

@bouviervj

Thank you for you great job - I was looking to implement the server version on the express-graphql stack here is my implementation as an example - not perfect but works:

     // schema is the initial schema you wanted to deploy

     schema = extendSchema(
      schema,
      lodashDirectiveAST,
    );

    app.use('/graphql', (...args) => {

      graphqlHTTP({
        schema: schema,
        graphiql: true,
        customParseFn : (source) => {

          // get the query from the body
          let body = source.body;

          // Process the lodash parsing
          let { query, transform } = graphqlLodash(body);

          // Replace the body in the Source instance
          source.body = query;

          // Use the standard parse method
          let documentAST = graphql.parse(source);

          // store the transform in the document, an element conveyed in customExecuteFn
          documentAST.transform = transform;

          return documentAST;

        },
        customExecuteFn : async ( { document, ...args } ) => {

          // Execut first
          let result = await graphql.execute( { document: document, ...args });

          // Then back transform the query.
          result.data = document.transform(result.data);

          // Return the result
          return result;

        }
      })(...args);

    });

  • cheers -

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions