Compiler plugin that supports GraphQL files in Meteor
meteor add swydo:graphql
# query.grahql
query getPerson ($id: ID!) {
person(id: $id) {
name
email
}
}
import query from './query.graphql';
// See https://github.com/apollographql/apollo-client for setup
const client = new ApolloClient();
// The query is parsed and can be directly passed to the Apollo Client
client.query({ query }).then(console.log);
It's also possible to define multiple queries in one file:
# queries.grahql
query foo {
baz
}
query bar {
baz
}
import { foo, bar } from './queries.graphql';
const client = new ApolloClient();
client.query({ query: foo }).then(console.log);
You can also import your main schema and pass it directly to makeExecutableSchema
.
# schema.graphql
"""
This is a description of a Person
This will show up in GraphiQL
"""
type Person {
id: ID!
name: String
email: String
}
type Query {
person(id!): Person
}
import { makeExecutableSchema } from 'graphql-tools';
import typeDefs from './schema.graphql'; // No need to parse it!
import resolvers from './resolvers';
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
The cool thing is that you can use import comments, that will import all definitions from another file:
#import "./personSchema.graphql"
type Query {
# It will recognize the Person type from the personSchema.graphql file
person(id): Person
}
We recommend to always use .graphql
, but also .graphqls
and .gql
files are supported.
There are some good reasons to use .graphql
files instead of the inline syntax:
- Good highlighting by GitHub and your IDE
- No need to manually parse strings with graphql-tag
- Small performance gain because queries and schemas are parsed ahead of time
- The GraphQL Configuration Protocol will support
.graphql
files out of the box - Works with babel-plugin-inline-import
Want to work with Meteor and GraphQL? Join the team!