1

I want to store my queries in an external file, let's say:

query GetAllTodos {
  todos{
    _id
    todoMessage
    createdAt
  }
}

query GetAllTests {
  tests
}

Is it possible somehow to load the entire file in a single string pass that string to ApolloClient and select what query I want to execute, eg:

apollo.query({query: gql([allQueries])}, select: 'TodoQuery').then(({data})=>data);

Is it possible to do this with any other client? not necessary apollo.

Marius
  • 3,253
  • 5
  • 25
  • 29

1 Answers1

0

I'm not aware of any way to do that specifically with existing tools.

That being said, you could make a module out of these queries (assuming you're using ES6+ and the package graphql-tag):

// queries.js
export const GetAllTodos = gql`...`
export const GetAllTests = gql`...`

// Where you do a query
import { GetAllTodos, GetAllTests } from './queries.js';
apollo.query({ query: GetAllTodos }).then(( { data } => data);

If you needed more dynamic-ness, the queries.js module could export a function that can map to these perhaps:

// queries.js
export const queries = {
  'GetAllTodos': gql`...`,
  'GetAllTests': gql`...`'
};

export default function selectQuery(key) {
  return queries[key];
}

// Where you do a query
import selectQuery from './queries.js';
apollo.query({ query: selectQuery('GetAllTodos') }).then(( { data } => data);

You could then expand on this to try and search for a query that matched 'TodoQuery' but name the queries something else like in your original example then.

pseudoramble
  • 2,541
  • 22
  • 28