Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active August 1, 2024 20:37
Show Gist options
  • Select an option

  • Save wilmoore/cc6747d02bfec454a6bbabd249856fc3 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/cc6747d02bfec454a6bbabd249856fc3 to your computer and use it in GitHub Desktop.
Software Engineering :: API :: Architectural Style :: GraphQL :: Training :: Learning GraphQL :: 4. Creating a GraphQL Server Schema

Software Engineering :: API :: Architectural Style :: GraphQL :: Training :: Learning GraphQL :: 4. Creating a GraphQL Server Schema

⪼ Made with 💜 by Polyglot.

A language which defines all of our APIs types

GraphQL Scalar Types

  • Int
  • Float
  • String
  • Boolean
  • ID
example
id: ID!
name: String!

GraphQL Object Types

Container of Scalar and other Object Types

type Photo {
  id: ID!
  name: String!
  url: String!
  description: String
  rating: Float
  private: Boolean!
}

Nullable vs. Non-nullable

!: value must be non-null

name: String!
description: String

Root Queries

type Query {
  totalUsers: Int!
}

Lists

type User {
  postedPhotos: [Photo!]!
}

Nullable vs. Non-nullable Lists

  • photos: [Photo]: Nullable list of nullable values
  • photos: [Photo]!: Non-nullable list of nullable values
  • photos: [Photo!]!: Non-nullable list of non-nullable values (empty array is valid)
» npm init -y
» npm install graphql apollo-server nodemon
» code .

index.js

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    totalDays: Int!
  }
`;

// const resolvers = {};

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server
  .listen()
  .then(({ url }) =>
    console.log(`Server running at ${url}`)
  );

package.json

{
  "name": "ski-day-counter",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "apollo-server": "^2.25.0",
    "graphql": "^15.5.0",
    "nodemon": "^2.0.7"
  }
}

Start Server

» npm start

image

image

Query for Mock Data

image

index.js

Define an Object Type

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type SkiDay {
    id: ID!
    date: String!
    mountain: String!
  }
  
  type Query {
    totalDays: Int!
    allDays: [SkiDay!]!
  }
`;

// const resolvers = {};

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server
  .listen()
  .then(({ url }) =>
    console.log(`Server running at ${url}`)
  );

Query allDays

query {
  totalDays
  allDays {
    id
    date
    mountain
  }
}

image

index.js

Adding an enumeration type

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type SkiDay {
    id: ID!
    date: String!
    mountain: String!
    conditions: Conditions
  }
  
  enum Conditions {
    POWDER
    HEAVY
    ICE
    THIN
  }
  
  type Query {
    totalDays: Int!
    allDays: [SkiDay!]!
  }
`;

// const resolvers = {};

const server = new ApolloServer({
  typeDefs,
  mocks: true,
});

server
  .listen()
  .then(({ url }) =>
    console.log(`Server running at ${url}`)
  );

Query conditions

query {
  totalDays
  allDays {
    id
    date
    mountain
    conditions
  }
}

image


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment