path | title |
---|---|
/learnings/learning_graphql |
Learning GraphQL |
The base of your GraphQL graph is a schema that holds the following things:
- Queries
- Mutations
- Subscriptions
All possible mutations, queries etc must be declared in this root object.
by this we mean that you can't just query random object types like you can in a relational database. Because you know the object/class exists does not mean you can just SELECT *
on it arbitrarily. (You must use the graph).
queries can specify object schema (optionally) by using 'fragment' sections of graphql query.
can also create interface whece you don't care what kind of object is returned but you know they mill all have same couple fields
query {
__schema {
types {
name
description
}
}
}
or better
query roots {
__schema {
queryType { ...typeFields }
mutationType { ...typeFields }
subscriptionType { ...typeFields }
}
}
fragment typeFields on __Type {
name
fields {
name
}
}
See also:
- LearningGraphQL_Apollo_Schema_Download
Is interesting because JSON doesn't support multiline strings.
Thus, you may need to join your strings into one before sending it.
POST https://somegrapql.api.example.com
content-type: application/json
{"query": "query roots { __schema { queryType { ...typeFields } mutationType { ...typeFields } subscriptionType { ...typeFields } } } fragment typeFields on __Type { name fields { name }} "}
every field needs one
- Learning GraphQL
Graph.cool is a self hosted GraphQL Backend As A Service.
It can be deployed on their infrastructure, and they also provide a command to spin up a cluster of Docker containers locally.
$ /usr/bin/graphcool local up
... local up
seems to use Docker Compose to do this, but I couldn't locate if they write / save the generated files anywhere.
It does however create a docker network: local_graphcool
.
$ npm install
$ graphql deploy --target local
GraphQL appears to use GraphQL to store and keep track of services. The root of Graph.cool is the following endpoint, which you can query via graphql (POST to the following endpoint) or view a web based playground if you GET it in a browser.
In fact, to debug issues in graphql you may have to turn on debug mode, note the executed queries, and manually execute them yourself.
$ DEBUG="*" graphql deploy --target local
Which will spit out GraphQL queries. Running these queries against the playground may help understand what's going on.
Validating that the GraphQL client schemas validate (and stay validating) with the server capabilites / queries
First, download the graphQL IDL/SDL file
get-graphql-schema looks like a good tool to download this in GraphQL SLD format
$ get-graphql-schema https://somegrapql.api.example.com > schema.graphql
Then validate your queries against the schmea validator using graphql-validator
This validator requires the schema to be in SDL, not JSON, format.
$ graphql-validator -s schema.graphql 'my_graphql_queries/**/*.graphl'
(make sure to use quotes: you acutally don't want the shell interpreting those quotes).
Apollo - from the Meteor people - are some good GraphqL tools.
Apollo, and its CLI tools, may give warnings about not supporting anoymous operations.
In fact, this is not just a warning, but your queries will fail validation if they are anoymous, even if they pass other GraphQL query validation steps.
You MUST give your queries names. While most of the time you can write
query {
getCars() {
name
}
}
With Apollo you must write it as so
query getCurrentUserCars {
getCars() {
name
}
}
(Noticed we have named the query??)
Source:
npm install apollo
<-- replaces other tools like apollo-code-gen
Some type based languages (TypeScript, Flow, Swift) will require / want you to add types to the data you get back, for type checking.
Remember that GraphQL the server specifies the things it can accept, and the client specifies the things it wants from the availiable types.
Meaning that if we have a "list the current user's cars" query. A particular screen in the client application may only care about the car's names.
Imagine this query can be created like so:
query getUserCars{
getUsersCars() {
name
}
}
This is a client side query. Our client application (an Angular 5 based application, for example) will want typesafty around that query.
Put this client side query in a seperate file and pull it into your program somehow so it is sent to the server. See Learning_GraphQL_Validation_CLI
First, download the generic schema from the GraphQL server ( <<LearningGraphQL_Apollo_Schema_Download>> )
$ apollo schema:download --endpoint https://api.graph.cool/simple/v1/example-project schema.json
$ apollo codegen:generate --schema=schema.json --target=swift --queries=./graphql.queries/*.graphql graphql
This will generate types for you, in this case in graphql/Types.graphql.swift
- Graph.cool lets you extend the schema in the source code. Per.
- "when a graphQL file contains no operation definitions types.swift is empty".