It’s now possible to import .graphql files directly in Meteor!
Installation
meteor add swydo:graphql
Usage
Create a .graphql file, like getPerson.graphql:
query getPerson {
person(id="1"): {
name
age
}
}
Now simply import it in your javascript:
import query from './getPerson.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);
You can also import your main schema and pass it directly to makeExecutableSchema:
import { makeExecutableSchema } from 'graphql-tools';
import typeDefs from './schema.graphql'; // No need to parse it!
import resolvers from './resolvers';
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
.graphql, .graphqls and .gql files are supported.
Benefits
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
Thanks for the info @vraptor. I haven’t tried it myself, but can you confirm if it returns just the string or an AST? If it’s just a string it will work with makeExecutableSchema, but maybe give some issues for normal queries. Not sure when you would use imports for queries though…
At the moment the server will error if you add an empty .graphql file. Most people will start with an empty file though so I think it should simple export nothing in that case instead of showing a parsing error.
What do you all think?
Edit: done! 0.0.3 will not stop your server when you add a new empty .graphql file!
Hi all, just an update on the current state of the package:
Field Descriptors
We added support for field descriptors, so this now works:
# This is just a normal comment
# This is a description of the type Person
# So you can see this in GraphiQL
type Person {
# This is a description of name
name: String
# And this says something about email
email: String
}
Error handling
The package now also handles parsing failures better in you .graphql files and reports them nicely, showing you the exact file, line number and column in your console.
As always, any feedback and bug reports are welcome.
I’ll need some more information, because import BaseSchemas from './BaseSchema.graphl should Just Work™
You don’t have to for types, although separating some of them is sometimes better to keep things organized. I do work with separate files for each query and mutation. You might end up with quite a few of them, but I don’t see the issue in that. I can add a query in a specific module, package or directory, close to where it is used. I wouldn’t want one big file with everything in it, just like I don’t want one file with all code in it.
Could you file an issue in the repo with details about package versions so I can reproduce? Or make a reproduction yourself that I can fork.
In general I would recommend reporting the issue in the repo itself
Edit: @a.com I just noticed you added export const BaseSchemas = [ ... ] around your schema, but you don’t have to do that in a .graphql file. This will for certain create errors. A .graphql file doesn’t need an export statement.
Also, did you know you can use import statements in a schema file?
#import "./personSchema.graphql"
type Query {
# It will recognize the Person type from the personSchema.graphql file
person(id): Person
}
import { foo, bar } from './queries.graphql';
const client = new ApolloClient();
client.query({ query: foo }).then(console.log);
String descriptors
It now also supports string descriptors, the official new way to add descriptions to your types. This means the example above will now look like this:
# This is just a normal comment
"""
This is a description of the Person type
So you can see this in GraphiQL
"""
type Person {
"This is a description of name"
name: String
"And this says something about email"
email: String
}
And a side note: the previous descriptors didn’t show up in GraphiQL. Sorry about that. These officially supported descriptors will actually show up. Enjoy!