Skip to content

Instantly share code, notes, and snippets.

@ermik
Created August 5, 2018 14:09
Show Gist options
  • Save ermik/db18c7cc7ec98680844e3fd90affeca9 to your computer and use it in GitHub Desktop.
Save ermik/db18c7cc7ec98680844e3fd90affeca9 to your computer and use it in GitHub Desktop.

Revisions

  1. ermik created this gist Aug 5, 2018.
    17 changes: 17 additions & 0 deletions endpoints.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // There are existing structs for Request -> Response dataflow as well as
    // a struct for the Product;
    //
    // In this case we are mapping the Input type to a Request type see yaml and grapqql files
    //
    // Here's the struct we map to:

    type CreateProductRequest struct {
    Name string `json:"name"`
    GTIN string `json:"gtin"`
    Description string `json:"description"`
    }

    // You can see that there is no mapping possible for 'clientMutationId'
    // In fact, this type is returned back to the caller, so it should be preserved
    // during query execution — in a resolver function mapping from CreareProductRequest
    // to a generated 'CreateProductPayload'
    6 changes: 6 additions & 0 deletions gqlgen.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    models:
    CreateProductInput:
    fields:
    clientMutationId:
    resolver: true
    model: <proprietary_repo>/endpoints.CreateProductRequest
    67 changes: 67 additions & 0 deletions schema.graphql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    type Product implements Node {
    """The ID of an object"""
    id: ID!
    }

    type Query {
    getProductsByName(name: String!, before: String, after: String, first: Int, last: Int): ProductConnection

    """Fetches an object given its Globally Unique ID"""
    node(
    """The ID of the object"""
    id: ID!
    ): Node
    }

    type Mutation {
    createProduct(input: CreateProductInput): CreateProductPayload
    }

    input CreateProductInput {
    clientMutationId: String!
    name: String!
    gtin: String!
    description: String!
    }

    type CreateProductPayload {
    clientMutationId: String!
    edge: ProductEdge
    }

    """An object with a Globally Unique ID"""
    interface Node {
    """The ID of the object."""
    id: ID!
    }

    type ProductConnection {
    """Information to aid in pagination."""
    edges: [ProductEdge]

    """Information to aid in pagination."""
    pageInfo: PageInfo!
    }

    type ProductEdge {
    """cursor for use in pagination"""
    cursor: String!

    """The item at the end of the edge"""
    node: Product
    }

    """Information about pagination in a connection."""
    type PageInfo {
    """When paginating forwards, the cursor to continue."""
    endCursor: String

    """When paginating forwards, are there more items?"""
    hasNextPage: Boolean!

    """When paginating backwards, are there more items?"""
    hasPreviousPage: Boolean!

    """When paginating backwards, the cursor to continue."""
    startCursor: String
    }