Giter Site home page Giter Site logo

graphql-go-pets-example's Introduction

Pets Example

This example is meant to show a full implementation of the server using an SQL datastore.

Starting

start the server

go run *.go

and then visit the GraphiQL dev server at localhost:8080

NOTES:

  • int32 maps to the graphql type Int, so if a number is desired, int32 must be used

  • the data structs are used for both database storage and graphql resolution.

  • resolver methods are all caps because the gorm ORM needs camel cased exported struct fields in order to create database columns correctly. Another differentiation scheme can be used if desired

  • authentication could go in middleware in the server part

  • authorization could be done with middleware, or from the user in the context after authentication in either the db methods or the resolvers. Official graphql recommends that these go into the data layer, but if a per-field authorization is needed, the field resolvers are the perfect place.

  • edges, connections, used for pagination

    • there is a type
    • it has a connection, which is another type that it is connected to
    • the connection happens through an edge which represents the relationship

Example queries

get user by ids with pets belonging to the user...

{
  getUser(id: 1) {
    name
    pets {
      name
      id
    }
  }
}

get pet by id and owner...

{
 getPet(id: 1) {
  name
  owner {
    name
    id
  }
}
}

insert pet with owner

mutation addPet($userID: Int!, $pet: PetInput!) {
  addPet(userID: $userID, pet: $pet) {
    name
  }
}

# query variables...
{
  "userID": 1,
  "pet": {
    "name": "slkdjsaldkjsalkj"
  }
}

update pet (needs query variables)

mutation UpdatePet($pet: PetInput!) {
  updatePet(pet: $pet) {
    name
    id
    owner {
      name
    }
    tags {
      title
    }
  }

}

delete pet (needs query variables)

mutation DeletePet($userID: Int!, $petID: Int!) {
  deletePet(userID: $userID, petID: $petID) {
    
  }
}

query Pet {
  one: getPet(id: 1) {
    name
    owner {
      name
    }
    tags {
      title
    }
  },
  two: 	getUser(id: 1) {
    name
  }
}

pagination ("after" or "before" can be added with the encoded cursor)

{
  getUser(id: 1) {
    name
    petsConnection(first: 2) {
      totalCount
      edges {
        cursor 
        node {
          name
        }
      }
    }
  }
}

graphql-go-pets-example's People

Contributors

andygarfield avatar jeffwillette avatar

Stargazers

 avatar

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.