How to use graphql-schema-bindings - 10 common examples

To help you get started, we’ve selected a few graphql-schema-bindings examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IBM / graphql-schema-bindings / examples / mutation / index.js View on Github external
@description('Delete a user from the array.')
  del(@arg(ID) @required id) {
    if (users[id]) {
      delete users[id];
      return true;
    }
    
    return false;
  }
}

/**
 * Now we are ready to create our ApolloServer and start listening for requests.
 */
const server = new ApolloServer({
  schema: createSchema(UserQuery, UserMutation),
//  context: new UserQuery()
});
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));
github IBM / graphql-schema-bindings / examples / twitter / src / types / Tweet.ts View on Github external
@field()
  text: string;

  @field()
  created_at: string;

  @field()
  retweet_count: number;

  @field()
  favorite_count: number;

  // Due to the circular reference between Tweet and User
  // the resolution of User needs to be deferred (via thunk function).
  @field(() => User)
  get user(): User {
    return new User(this.data.user);
  }

  /**
   * Create an instance of the Tweet output type.
   * @param data Tweet data from the API.
   */
  constructor(data: ITweet) {
    this.data = data;
    this.id = data.id_str;
    this.text = data.text;
    this.created_at = data.created_at;
    this.retweet_count = data.retweet_count;
    this.favorite_count = data.favorite_count;
  }
github IBM / graphql-schema-bindings / examples / mutation / index.js View on Github external
@context
  context;
  
  @field(User)
  @description('Get a user from the array.')
  add(@arg(ID) @required id, @arg(String) @required name) {
    const user = new User({
      id,
      name,
    });
    users[id] = user;
    return user;
  }
  
  @field(Boolean)
  @description('Delete a user from the array.')
  del(@arg(ID) @required id) {
    if (users[id]) {
      delete users[id];
      return true;
    }
    
    return false;
  }
}

/**
 * Now we are ready to create our ApolloServer and start listening for requests.
 */
const server = new ApolloServer({
  schema: createSchema(UserQuery, UserMutation),
//  context: new UserQuery()
github IBM / graphql-schema-bindings / examples / xkcd / index.js View on Github external
@type
class XKCDQuery {
  @field(Comic)
  async comic(@arg(ID) id) {
    const url = id ? `/${id}` : "";
    const { data } = await axios.get(`https://xkcd.com${url}/info.0.json`);
    return new Comic(data);
  }

  @field(Comic)
  latest() {
    return this.comic();
  }

  @field(Comic)
  async random() {
    const { number } = await this.comic();
    return this.comic(Math.floor(Math.random() * number));
  }
}

const server = new ApolloServer({
  schema: createSchema(XKCDQuery),
  context: new XKCDQuery()
});
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));
github IBM / graphql-schema-bindings / examples / xkcd / index.js View on Github external
}

  @field(Comic)
  latest() {
    return this.comic();
  }

  @field(Comic)
  async random() {
    const { number } = await this.comic();
    return this.comic(Math.floor(Math.random() * number));
  }
}

const server = new ApolloServer({
  schema: createSchema(XKCDQuery),
  context: new XKCDQuery()
});
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));
github IBM / graphql-schema-bindings / examples / github_users / index.js View on Github external
* This is our query class.  It is the entry point for all queries to our application.
 */
@type
class UserQuery {
  @field(User)
  async user(@arg(ID) id) {
    const { data } = await axios.get(`https://api.github.com/users/${id}`);
    return new User(data);
  }
}

/**
 * Now we are ready to create our ApolloServer and start listening for requests.
 */
const server = new ApolloServer({
  schema: createSchema(UserQuery),
  context: new UserQuery()
});
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));
github IBM / graphql-schema-bindings / examples / twitter / src / index.ts View on Github external
import { ApolloServer } from "apollo-server";
import { createSchema } from "graphql-schema-bindings";
import axios from "axios";
import Twitter from "./Twitter";
import getToken from "./lib/getToken";

const { TWITTER_KEY, TWITTER_SECRET } = process.env;
if (!TWITTER_KEY || !TWITTER_SECRET) {
  throw new Error(
    "Environment variables must be set: TWITTER_KEY, TWITTER_SECRET."
  );
}

export const server = new ApolloServer({
  schema: createSchema(Twitter),
  context: async () => ({
    client: axios.create({
      baseURL: "https://api.twitter.com/1.1",
      headers: { Authorization: `Bearer ${await getToken()}` }
    }),
    twitter: new Twitter()
  })
});

async function main() {
  const { url } = await server.listen();
  console.log(`Server listening at ${url}`);
}

if (!module.parent) {
  main();
github IBM / graphql-schema-bindings / examples / mutation / index.js View on Github external
@field([User])
  @description('Get all of the users in the array.')
  users() {
    return users;
  }
}

/**
 * This is our mutation class.  It makes it possible to make changes to our data.
 */
@type
class UserMutation {
  @context
  context;
  
  @field(User)
  @description('Get a user from the array.')
  add(@arg(ID) @required id, @arg(String) @required name) {
    const user = new User({
      id,
      name,
    });
    users[id] = user;
    return user;
  }
  
  @field(Boolean)
  @description('Delete a user from the array.')
  del(@arg(ID) @required id) {
    if (users[id]) {
      delete users[id];
      return true;
github IBM / graphql-schema-bindings / examples / github_users / index.js View on Github external
import {
  arg,
  context,
  createSchema,
  description,
  field,
  ID,
  type
} from "graphql-schema-bindings";

/**
 * Our very simple User class just represents the user ID and the name
 */
@type
class User {
  @field(ID)
  get id() {
    return this.data.id;
  }
  
  @field(String)
  @description('This is the name of the current user')
  get name() {
    return this.data.name;
  }

  constructor(data) {
    this.data = data;
  }
}

/**
github IBM / graphql-schema-bindings / examples / xkcd / index.js View on Github external
@field(String)
  get transcript() {
    return this.data.transcript || this.data.alt;
  }

  @field(String)
  get date() {
    return new Date(
      this.data.year,
      this.data.month,
      this.data.day
    ).toLocaleDateString();
  }

  @field(String)
  get link() {
    return `https://xkcd.com/${this.number}`;
  }

  @field(Comic)
  async previous() {
    return this.context.comic(this.number - 1);
  }

  @field(Comic)
  async next() {
    return this.context.comic(this.number + 1);
  }

  constructor(data) {
    this.data = data;

graphql-schema-bindings

an opinionated graphql server format for combining schema and implementation

Apache-2.0
Latest version published 1 year ago

Package Health Score

49 / 100
Full package analysis

Similar packages