tutorial.md

April 24, 2021 ยท View on GitHub

Tutorial

This tutorial is for developers who want to know how to use nim-graphql and how to write graphql service using nim-graphql. This tutorial doesn't talk about how the server or the client should be written. That topic is covered in server section.

This tutorial covers about:

  • loading schema
  • implementing resolvers
  • parsing variables
  • executing queries

Important notes

In nim-graphql, both queries and schemas share the same pool of Names, after loading and executing a query, we usually discard the recent query and keep schemas intact.

Every time we load/execute a new query, there is possibility it create new Names that is not needed by the system. To remove this unused Name, we will call purgeNames(counter). purgeNames will remove all Names with id greater than the counter parameter.

This is a simplified example using Nim-like syntax to describe how to use purgeNames

  var server = setupServer()
  let ctx = GraphqlRef.new()
  ctx.parseSchemaFromFile("myschema.ql")
  let counter = ctx.getNameCounter()

  loop:
    let query = server.recvQuery()

    # one or more queries can be executed
    ctx.parseQuery(query)
    let resp = JsonRespStream.new()
    ctx.executeRequest(resp, "operationName")
    server.sendResponse(resp)

    ctx.purgeQueries() # remove recent queries
    ctx.purgeNames(counter) # remove all names associated with recent queries

The server can decide how much queries executed before calling purgeNames.

Star Wars tutorial