Skip to content

Latest commit

 

History

History
203 lines (169 loc) · 2.93 KB

_using-the-graphql-api.md

File metadata and controls

203 lines (169 loc) · 2.93 KB

Using the GraphQL API

The schema that specifies the API operations of your GraphQL server is defined in ./schema.graphql. Below are a number of operations that you can send to the API using the GraphQL Playground.

Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.

Retrieve all published posts and their authors

query {
  feed {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}
See more API operations

Retrieve the drafts of a user

{
  draftsByUser(
    userUniqueInput: {
      email: "[email protected]"
    }
  ) {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}

Create a new user

mutation {
  signupUser(data: { name: "Sarah", email: "[email protected]" }) {
    id
  }
}

Create a new draft

mutation {
  createDraft(
    data: { title: "Join the Prisma Discord", content: "https://pris.ly/discord" }
    authorEmail: "[email protected]"
  ) {
    id
    viewCount
    published
    author {
      id
      name
    }
  }
}

Publish/unpublish an existing post

mutation {
  togglePublishPost(id: __POST_ID__) {
    id
    published
  }
}

Note that you need to replace the __POST_ID__ placeholder with an actual id from a Post record in the database, e.g.5:

mutation {
  togglePublishPost(id: 5) {
    id
    published
  }
}

Increment the view count of a post

mutation {
  incrementPostViewCount(id: __POST_ID__) {
    id
    viewCount
  }
}

Note that you need to replace the __POST_ID__ placeholder with an actual id from a Post record in the database, e.g.5:

mutation {
  incrementPostViewCount(id: 5) {
    id
    viewCount
  }
}

Search for posts that contain a specific string in their title or content

{
  feed(
    searchString: "prisma"
  ) {
    id
    title
    content
    published
  }
}

Paginate and order the returned posts

{
  feed(
    skip: 2
    take: 2
    orderBy: { updatedAt: desc }
  ) {
    id
    updatedAt
    title
    content
    published
  }
}

Retrieve a single post

{
  postById(id: __POST_ID__ ) {
    id
    title
    content
    published
  }
}

Note that you need to replace the __POST_ID__ placeholder with an actual id from a Post record in the database, e.g.5:

{
  postById(id: 5 ) {
    id
    title
    content
    published
  }
}

Delete a post

mutation {
  deletePost(id: __POST_ID__) {
    id
  }
}

Note that you need to replace the __POST_ID__ placeholder with an actual id from a Post record in the database, e.g.5:

mutation {
  deletePost(id: 5) {
    id
  }
}