Class: DemoApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/demo_api/client.rb

Overview

Thin client over the demo GraphQL API.

Each method sends one operation and returns the plain data hash. The shapes are documented in the GraphQL reference, linked from every method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, token: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • endpoint (String) —

    URL of the GraphQL endpoint

  • token (String, nil) (defaults to: nil) —

    bearer token sent in the Authorization header



30
31
32
33
# File 'lib/demo_api/client.rb', line 30

def initialize(endpoint:, token: nil)
  @endpoint = URI(endpoint)
  @token = token
end

Instance Attribute Details

#endpoint ⇒ URI::HTTP (readonly)

Returns the GraphQL endpoint.

Returns:

  • (URI::HTTP) —

    the GraphQL endpoint



26
27
28
# File 'lib/demo_api/client.rb', line 26

def endpoint
  @endpoint
end

Instance Method Details

#create_project(input) ⇒ Hash

Creates a project.

Sends the createProject mutation.

Parameters:

Returns:

Raises:

  • (Error) —

    when the API rejects the input



84
85
86
87
88
89
90
91
# File 'lib/demo_api/client.rb', line 84

def create_project(input)
  query = <<~GRAPHQL
    mutation CreateProject($input: CreateProjectInput!) {
      createProject(input: $input) { id name status }
    }
  GRAPHQL
  execute(query, { input: input })['createProject']
end

#folder(id) ⇒ Hash?

Fetches a folder by id.

Sends the folder query, which returns a Folder. Both have their own page: the flat hierarchy prefixes the kind to each name.

Parameters:

  • id (String) —

    the folder’s ID

Returns:

  • (Hash, nil) —

    the folder, or nil when it does not exist



65
66
67
68
69
70
71
72
# File 'lib/demo_api/client.rb', line 65

def folder(id)
  query = <<~GRAPHQL
    query Folder($id: ID!) {
      folder(id: $id) { id name parent createdAt }
    }
  GRAPHQL
  execute(query, { id: id })['folder']
end

#projects(status: nil, limit: 20) ⇒ Array<Hash>

Lists projects, optionally filtered by lifecycle stage.

Sends the projects query.

Parameters:

  • status (Symbol, nil) (defaults to: nil) —

    a ProjectStatus value, such as :active

  • limit (Integer) (defaults to: 20) —

    maximum number of projects to return

Returns:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/demo_api/client.rb', line 45

def projects(status: nil, limit: 20)
  query = <<~GRAPHQL
    query Projects($status: ProjectStatus, $limit: Int) {
      projects(status: $status, limit: $limit) {
        nodes { id name status updatedAt }
      }
    }
  GRAPHQL
  variables = { status: status&.to_s&.upcase, limit: limit }
  execute(query, variables).dig('projects', 'nodes')
end