05/04/2021 - graphql server
this is the javascript nodejs express server side implementation
i wanted something similar very close to just nodejs only and no apollo to avoid adding packages
but majority of tutorials on the net use apollo
purpose of graphql
- implementation agnostic, flexible
- easier to do more complicated queries
- self documenting
- what you see if what you get
- reduce proliferation of endpoints
- client side can define their own data api
- so graphql will add a layer of customisation help you do joins and nested queries
reading
- sdl/ast/syntax/language - https://graphql.org/learn/
- schemas and types
- queries and mutations
- js implementation - https://graphql.org/graphql-js/
- running express and graphql
- clients
youtube tutorial applying the above
https://www.youtube.com/watch?v=Vs_CBxCfFHk
https://www.youtube.com/watch?v=Vs_CBxCfFHk
express-graphql npm
file structure for backend
- your server file
- express-graphql, graphhttp
- schema file
- gql tag or template strings
- resolver file
- plain js functions
- or you might even want to split up the schemas or resolvers into different files.
- mongoose models schema
- you still need those
file structure for the frontend (react, redux)
- use fetch or axios
- the endpoint is 'api/graphql', the single route you created with express-graphql
code
- backend
- schema for objects, interfaces, queries and mutations
- resolvers
- the package's graphhttp accepts those two above
- the package ships with graphiql for you to test queries
- front end
- queries, variables
- redux actions and fetch or axios to fetch data from the endpoint
- schemas and types - declaring using the graphql syntax or sdl
- theres more than one way to do it, and the sdl way is the native way
- write in SDL way in graphql.org/learn,
- dont use the npm graphql way import graphqlobjecttypes
- understanding types and schema - types and schema vs REST and MVC
- M for model in MVC - types you need - object, scalars
- graphql object type aka documents or entities
- syntax: `type [name]{...}`
- graphql scalars type (string, bools etc), concrete
- String,Int, Boolean,[],ID
- they also have enums
- they are used in the document fields to set the data type
- queries,mutations, inputs
- mutations are update/delete/create or post/put/delete of CRUD and rest api requests
- inputs are objects to go into mutation args, less messy code
- queries are read/get of crud/rest
- additional
- validation
- ! in graphql means a field is required aka nonnull
- directives and fragments
- @include directive can be used for user auth i guess
- interface, unions, fragments
- interface are more abstract objects eg person vs user, star wars character vs human/droid, you use the interface type and the implement keyword
- declare interface type
- new object type extends interface type
- fragments are reusable units
- use the fragment declaration and the on keyword, and the spread ... operator to include the fragment in the type schema body
- inline fragment
- a little confusing
- but if you do a query type
- considerations
- since the sdl is not js, but there is still a way to work around without packages
- use template strings
- gql tag
- use the {print} in graphql to convert ast to string
- vscode extensions
- graphql extension
- reads .graphql file, or gql`...` strings as graphql, you get the colorisation and formatting with prettier
- hack, you can temporarily append the gql tag to your template string without adding the graphql tag package, make sure you remove it later on
- to modularise your code
- export a const containing the string
- if you have multiple separate schemas, you can use .join()
- resolvers
- plain javascript functions
- much like the body of rest api controller
- you can bring in your mongo/mongoose queries at this level
- find, findOne, create etc
- make use of js filter higher order function
- queries
- on the schema
- args, name of the object type they return eg. course, post, comment
- on the frontend/graphiql
- names- you can name that query for error tracing
- $variables - you don't need to hardcode the values coming from your frontend, define variables with the $ prefix.
theres a cheatsheet here for the SDL
picture notes from vscode
o schema
note that you have the add the gql tag to read it as graphql and remove it to turn it back into js template strings
o resolvers aka root
o
o
picture notes from graphql.org/learn
o input and mutation
o working with interfaces (abstract objects) and inline fragments
//backend
todos
mongo queries
user auth
pagination
//frontend
edit redux action to use the single endpoint and add in graphql queries
additional readings
article on how to use graphql with mongodb
Comments