Git Product home page Git Product logo

go-gqlgen-sample's Introduction

This is a crash course in GraphQL using a schema-first approach (generated code).

First things first:

go get github.com/99designs/gqlgen

This repo was started with gql and yml files so we don't run init. Instead we run

go run github.com/99designs/gqlgen

This looks for the yml file (gqlgen.yml in our case) and generates models and the code that maps the GQL parser to the resolver functions that we need to implement.

The yaml file has resolver.go set so that's where code first went. When it was created it was filled with function stubs that contained panic statements. The panic statements have been replaced with some basic server functions. Go structs allow interface methods to be defined in other files, but a nice convention is to leave the struct declarations in there.

This Go directive is at the top of resolver.go

//go:generate go run github.com/99designs/gqlgen

This directive simplifies command line usage to:

$ go generate

A mod file was defined and with the generated output in place we can fill a vendor directory with our dependencies:

$ go mod vendor

Which creates the vendor subdirectory and fills it with delicious code units.

Run the server with:

go run server/server.go

Then browse to:

http://localhost:8080/graphql

If everything went well you should see a dark themed developer playground. The server is running from memory, so a fresh instance has no data.

This sample project has defined Articles and Users.

from gql.article:

...
input NewArticle {
  text: String! # content is required
  userID: Int!  # userID is required
}
...

To create an Article we will need to use the NewArticle object, which requires a userID attribute. This means we need to first create a user.

Copy this snippet of GQL into the playground:

mutation {
  newUser(input: {
    name:"Code Smith"
  }){
    id
  }
}

This is parsed by the code in generated.go and calls NewUser(ctx context.Context, input NewUser) in resolver.go which returns a User, but we have only requested the id attribute to be returned.

expected response:

{
  "data": {
    "newUser": {
      "id": 0
    }
  }
}

Now we have a userID can create a new Article:

mutation {
  newArticle(input: {
    text:"When you have a hammer everything is a nail. When you have a smith everything is anything you want it to be."
    userID: 0
  }){
    id
    text
    user {
      name
    }
  }
}

You should now have output that looks like:

{
  "data": {
    "newArticle": {
      "id": 0,
      "text": "When you have a hammer everything is a nail. When you have a smith everything is anything you want it to be.",
      "user": {
        "name": "Code Smith"
      }
    }
  }
}

You have now been introduced to everything you need to create new mutation query. Try changing the text of the article text to anything you want it to be.

Inspect your work with these simple queries:

query {
  articles {
    id
    text
    user {
      ...
    }
  }
}
query {
  article(id:0) {
    text
    user {
      id
      name
    }
  }
}
query {
  user(id: 0) {
    name
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.