3

I have created a basic GraphQL Express app and I'm wanting to bundle pre defined data from pre defined queries with specific routes.

Ideally the query should allow providing arguments so it can be used flexibly, I would like to be able to save the query to a file and run it on demand but provide arguments specific to the current data required.

I can query the api with the following query

query authors(ids: [1337, 42]) {
  name,
  id
}

The query.graphql file should be something like the following:

getAuthorsById($ids: Int[]) {
  authors(ids: $ids) {
    name,
    id
  }
}

What I want to do within the Node server is get the content from a query.graphql file and execute it when a specific route is fired eg.

const query = somehowImportTheQuery('./query.graphql')
graphql(schema, query([1337, 42]))

The above code somehowImportTheQuery should import the query and return a function getAuthorsById that can be called with arguments.

Does something like this already exist? or are there any tools or documentation that can help me achieve the desired functionality?

Thanks for any help!

1 Answer 1

4

You could use documents-loading of graphql-tools module to load GraphQL operation documents from different sources.

E.g.

index.ts:

import { GraphQLSchema, buildSchema, graphql } from 'graphql';
import { loadDocumentsSync, GraphQLFileLoader } from 'graphql-tools';
import path from 'path';

const typeDefs: string = `
    type Author {
        id: ID!
        name: String
    }
    type Query {
        authors(ids: [ID]!): [Author]!
    }
`;
const resolvers = {
  authors({ ids }) {
    return [
      { id: ids[0], name: 'a' },
      { id: ids[1], name: 'b' },
    ];
  },
};

const schema: GraphQLSchema = buildSchema(typeDefs);

const query = loadDocumentsSync(path.resolve(__dirname, './query.graphql'), {
  loaders: [new GraphQLFileLoader()],
});

graphql({
  schema,
  source: query[0].rawSDL!,
  rootValue: resolvers,
  variableValues: { ids: [1337, 42] },
}).then(({ data }) => {
  console.log(data);
});

query.graphql:

query getAuthorsById($ids: [ID]!) {
  authors(ids: $ids) {
    name
    id
  }
}

The execution result:

[Object: null prototype] {
  authors:
   [ [Object: null prototype] { name: 'a', id: '1337' },
     [Object: null prototype] { name: 'b', id: '42' } ] }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the great answer, that's perfect. I already made a basic file loader but it's probably best to use the graphql-tools like you have suggested.
I want to fetch data from github graphql api with a file query.graphql that is nicely formatted, so where is the fetch to send the query as post data and then fetch the result of the query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.