Skip to content

Learning graphql relay in NodeJS basic properties, non database connection

Notifications You must be signed in to change notification settings

jdgabriel/learning-graphql-relay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b0e71fd · Jun 26, 2024

History

3 Commits
Jun 26, 2024
Jun 26, 2024
Jun 26, 2024
Jun 26, 2024
Jun 26, 2024
Jun 26, 2024
Jun 26, 2024

Repository files navigation

API GraphQL with Relay

Learning graphql relay in NodeJS basic properties, non database connection

Post Type

export const PostType = new GraphQLObjectType({
  name: "Post",
  description: "Post type properties",
  fields: () => ({
    id: globalIdField("Posts"), // <-- Global id for use in relay
    title: {
      type: GraphQLString,
      resolve: (post) => post.title,
    },
    body: {
      type: GraphQLString,
      resolve: (post) => post.body,
    },
  }),
  interfaces: [nodeInterface], // <- Node Interface
});

// Relay connection and edge
export const { connectionType: PostConnection, edgeType: PostEdge } = connectionDefinitions({
  nodeType: PostType,
});

Node Interface

export const { nodeInterface, nodeField } = nodeDefinitions(
  (globalId) => {
    const { type, id } = fromGlobalId(globalId);
    /**
     * type -> Type of global id (e.g: Post)
     * id   -> Identification id (e.g: uuid)
     * Search for data where contains equal id in database or local array
     */
  },
  (obj) => {
    /**
     * obj    -> Contains all properties returned from globalId function
     * return -> Return type of data (e.g: Post)
     */
    return "Post";
  }
);

Query

export const QueryType = new GraphQLObjectType({
  name: "Query",
  description: "Query type root",
  fields: () => ({
    node: nodeField // <-- Add Node Resolver query,
    post: {
      type: new GraphQLNonNull(PostConnection), // <-- PostConnection Relay
      args: connectionArgs,
      resolve: async (_, args, ctx) => {
        const data = await PostLoader.loadAll();
        return connectionFromArray(data, args); // <-- Pagination Relay
      },
    },
  }),
});

Mutation

export const MutationType = new GraphQLObjectType({
  name: "Mutation",
  description: "Mutation types",
  fields: () => ({
    // Add all mutations functions
  }),
});

Server Schema

export const schema = new GraphQLSchema({
  query: QueryType,
  mutation: MutationType,
  // subscription: SubscriptionType
});

About

Learning graphql relay in NodeJS basic properties, non database connection

Topics

Resources

Stars

Watchers

Forks