Skip to content

quaos/deno-fetch-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deno Fetch GraphQL

A lightweight client for creating and sending GraphQL requests over Fetch API (without Schema/GQL Types validation)

Examples

import {
  GraphQLClient,
  GraphQLRequest,
  OperationType,
} from "https://deno.land/x/fetch_graphql@v1.0.0/mod.ts";

interface HelloInputVars {
  myName: string;
}

const helloQuery = {
    operationType: OperationType.Query,
    name: "Hello",
    variableDefs: [
      {
        name: "myName",
        graphqlType: "string!",
      },
    ],
    field: {
      name: "hello",
      args: [
        {
          name: "from",
          fromVariable: "myName",
        }
      ],
      children: [
        "message",
        "andMyNameIs",
        {
          name: "yourInfo",
          children: [
            "ipAddress",
            "lastLoginAt",
          ]
        }
      ],
  }
};

const countUpMutation = {
  operationType: OperationType.Mutation,
  field: {
    name: "countUp",
    args: [
      {
        name: "increment",
        value: 10,
        valueType: "Int!",
      }
    ],
  }
};

const client = new GraphQLClient({ url: "http://myserver:8080/graphql" });
const gqlRequest = new GraphQLRequest<HelloInputVars>()
  .add(helloQuery)
  .add(countUpMutation)
  .withAuthToken("jwt01234abcd")
  .withVariables({
    myName: "Q",
  });

const gqlResponse = await client.send({
  operationType: OperationType.Query
});

interface HelloResult {
  message: string;
  andMyNameIs: string;
}

if (gqlResponse.hasError()) {
  gqlResponse.errors.forEach(console.error);
  gqlResponse.throwFirstError();
}

const result = gqlResponse.getData<HelloResult>(helloQuery);
console.log(result);

About

A lightweight client for creating and sending GraphQL requests over Fetch API (without Schema/GQL Types validation)

Resources

Stars

Watchers

Forks

Packages

No packages published