hongbo-miao/hongbomiao.com

View on GitHub
api-node/src/graphQL/queries/query.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { GraphQLID, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType } from 'graphql';
import FibonacciGraphQLType from '../graphQLTypes/FibonacciGraphQLType';
import MeGraphQLType from '../graphQLTypes/MeGraphQLType';
import PlanetGraphQLType from '../graphQLTypes/PlanetGraphQLType';
import StarshipGraphQLType from '../graphQLTypes/StarshipGraphQLType';
import UserGraphQLType from '../graphQLTypes/UserGraphQLType';
import getFibonacci from '../utils/getFibonacci';
import getMe from '../utils/getMe';

const query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    fibonacci: {
      type: FibonacciGraphQLType,
      args: {
        n: { type: new GraphQLNonNull(GraphQLInt) },
      },
      resolve: (parentValue, args) => {
        const { n } = args;
        return getFibonacci(n);
      },
    },
    me: {
      type: MeGraphQLType,
      resolve: async () => {
        return getMe();
      },
    },
    user: {
      type: UserGraphQLType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve: async (parentValue, args, context) => {
        const { id } = args;
        const { dataLoaders } = context;
        return dataLoaders.user.load(id);
      },
    },
    users: {
      type: new GraphQLList(UserGraphQLType),
      args: {
        ids: { type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID))) },
      },
      resolve: async (parentValue, args, context) => {
        const { ids } = args;
        const { dataLoaders } = context;
        return dataLoaders.user.loadMany(ids);
      },
    },
    planet: {
      type: PlanetGraphQLType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve: async (parentValue, args, context) => {
        const { id } = args;
        const { dataLoaders } = context;
        return dataLoaders.planet.load(id);
      },
    },
    planets: {
      type: new GraphQLList(PlanetGraphQLType),
      args: {
        ids: { type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID))) },
      },
      resolve: async (parentValue, args, context) => {
        const { ids } = args;
        const { dataLoaders } = context;
        return dataLoaders.planet.loadMany(ids);
      },
    },
    starship: {
      type: StarshipGraphQLType,
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) },
      },
      resolve: async (parentValue, args, context) => {
        const { id } = args;
        const { dataLoaders } = context;
        return dataLoaders.starship.load(id);
      },
    },
    starships: {
      type: new GraphQLList(StarshipGraphQLType),
      args: {
        ids: { type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID))) },
      },
      resolve: async (parentValue, args, context) => {
        const { ids } = args;
        const { dataLoaders } = context;
        return dataLoaders.starship.loadMany(ids);
      },
    },
  },
});

export default query;