Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (85 sloc) 2.14 KB
description
This page refers to the GraphQL JavaScript package

GraphQL

Directory Structure

src/graphql/Schema.ts
src/graphql/Mutation.ts
src/graphql/User.ts (GraphQLObjectType)
src/graphql/mutations/UserMutation.ts

Schema

Put edges in a "Viewer" GraphQLObjectType so that you can use it as the "root" object.

type Query {
  entries(order: String, sort: String, after: String, first: Int, before: String, last: Int): EntryConnection
  tags(after: String, first: Int, before: String, last: Int): TagConnection
  viewer: Viewer
}

type Viewer implements EntryConnectionInterface {
  entries(order: String, sort: String, after: String, first: Int, before: String, last: Int): EntryConnection
  tags(after: String, first: Int, before: String, last: Int): TagConnection
}

Resolvers

Naming:

  • Postfix function names with Resolver
  • Pass in all the args: root, args, context, info

{% code-tabs %} {% code-tabs-item title="Entry.ts" %}

// For the connections
`query {
  entries(...) {
     edges...
  }
}`
export async function EntriesResolver(root, args, context, info);

`query {
  entry(...) { }
}`
export async function EntryResolver(root, args, context, info);

// My suggestion is put this function in the `Tag.ts` file.
// The goal is put all Resolvers that query the same database in the same file
`query {
  entry(...) {
    tags(...) { 
      edges...
    }
  }
}`
export async function EntryTagsResolver(root, args, context, info) {
  const row = await knex
    .from("tags")
    .where({
      tag.entryID: root.id,
    })
    .select();
    
  ...
}

{% endcode-tabs-item %} {% endcode-tabs %}

export async function EntryResolver(
  root,
  args,
  context: Context,
  info
): Promise<any> {
  const userUUID = getUserUUIDFromContext(context);

  const where = {
    uuid: args.uuid,
    createdBy: userUUID
  };

  const row = await knex
    .from("users")
    .where(where)
    .limit(1)
    .select();
  return row.shift();
}
You can’t perform that action at this time.