Modules@robinpath/graphql
graphql

@robinpath/graphql

0.1.1Node.jsPublic

GraphQL client with queries, mutations, variables, and introspection

GraphQL

GraphQL client with queries, mutations, variables, introspection, batch requests, and query builder

Package: @robinpath/graphql | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the graphql module when you need to:

  • Create a named GraphQL client -- Use graphql.create to perform this operation
  • Execute a GraphQL query -- Use graphql.query to perform this operation
  • Execute a GraphQL mutation -- Use graphql.mutate to perform this operation
  • Send a one-off GraphQL request without creating a client -- Use graphql.rawRequest to perform this operation
  • Run an introspection query to discover the schema -- Use graphql.introspect to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate a named GraphQL client{name, endpoint}
queryExecute a GraphQL queryQuery data
mutateExecute a GraphQL mutationMutation result
rawRequestSend a one-off GraphQL request without creating a clientFull response {data, errors}
introspectRun an introspection query to discover the schemaSchema introspection result
listTypesList all types in the GraphQL schemaArray of {name, kind, description}
buildQueryBuild a simple GraphQL query string from partsGraphQL query string
batchQueryExecute multiple queries sequentiallyArray of {data, error}
destroyRemove a GraphQL clientTrue if removed

Functions

create

Create a named GraphQL client

Module: graphql | Returns: object -- {name, endpoint}

graphql.create "github" "https://api.github.com/graphql" {"token": $ghToken}
ParameterTypeRequiredDescription
namestringYesClient name
endpointstringYesGraphQL endpoint URL
optionsobjectNo{token, apiKey, headers}

query

Execute a GraphQL query

Module: graphql | Returns: object -- Query data

graphql.query "github" "{ viewer { login name } }"
ParameterTypeRequiredDescription
clientstringYesClient name
querystringYesGraphQL query string
variablesobjectNoQuery variables
optionsobjectNo{operationName, ignoreErrors, raw, headers}

mutate

Execute a GraphQL mutation

Module: graphql | Returns: object -- Mutation result

graphql.mutate "api" "mutation { createUser(name: $name) { id } }" {"name": "Alice"}
ParameterTypeRequiredDescription
clientstringYesClient name
mutationstringYesGraphQL mutation string
variablesobjectNoMutation variables

rawRequest

Send a one-off GraphQL request without creating a client

Module: graphql | Returns: object -- Full response {data, errors}

graphql.rawRequest "https://api.example.com/graphql" "{ users { id } }"
ParameterTypeRequiredDescription
endpointstringYesGraphQL URL
querystringYesQuery string
variablesobjectNoVariables
headersobjectNoCustom headers

introspect

Run an introspection query to discover the schema

Module: graphql | Returns: object -- Schema introspection result

graphql.introspect "github"
ParameterTypeRequiredDescription
clientstringYesClient name

listTypes

List all types in the GraphQL schema

Module: graphql | Returns: array -- Array of {name, kind, description}

graphql.listTypes "github"
ParameterTypeRequiredDescription
clientstringYesClient name

buildQuery

Build a simple GraphQL query string from parts

Module: graphql | Returns: string -- GraphQL query string

graphql.buildQuery "query" "getUser" ["id", "name", "email"] {"id": "ID!"}
ParameterTypeRequiredDescription
typestringYes'query' or 'mutation'
namestringYesOperation name
fieldsarrayYesFields to select
variablesobjectNo{varName: 'Type'}

batchQuery

Execute multiple queries sequentially

Module: graphql | Returns: array -- Array of {data, error}

graphql.batchQuery "api" $queries
ParameterTypeRequiredDescription
clientstringYesClient name
queriesarrayYesArray of {query, variables}

destroy

Remove a GraphQL client

Module: graphql | Returns: boolean -- True if removed

graphql.destroy "github"
ParameterTypeRequiredDescription
clientstringYesClient name

Error Handling

All functions throw on failure. Common errors:

ErrorCause
GraphQL endpoint URL is requiredCheck the error message for details
GraphQL error: ${firstError.message}Check the error message for details
GraphQL client "..." not found. Create it first.Check the error message for details
@desc "Create and validate result"
do
  set $result as graphql.create "github" "https://api.github.com/graphql" {"token": $ghToken}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Types

Retrieve all items and loop through them.

@desc "List types and iterate results"
do
  set $result as graphql.listTypes "github"
  each $item in $result
    print $item
  end
enddo

2. Create a new item with create

Create a new resource and capture the result.

set $result as graphql.create "github" "https://api.github.com/graphql" {"token": $ghToken}
print "Created: " + $result

3. Check before creating

List existing items and only create if needed.

@desc "List types and create"
do
  set $existing as graphql.listTypes "github"
  if $existing == null
    graphql.create "github" "https://api.github.com/graphql" {"token": $ghToken}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step GraphQL workflow

Chain multiple graphql operations together.

@desc "Create, query, and more"
do
  set $r_create as graphql.create "github" "https://api.github.com/graphql" {"token": $ghToken}
  set $r_query as graphql.query "github" "{ viewer { login name } }"
  set $r_mutate as graphql.mutate "api" "mutation { createUser(name: $name) { id } }" {"name": "Alice"}
  print "All operations complete"
enddo

5. Safe create with validation

Check results before proceeding.

@desc "Create and validate result"
do
  set $result as graphql.create "github" "https://api.github.com/graphql" {"token": $ghToken}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/graphql

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.4 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web