/wp-graphql-polls

A GraphQL interface for WP-Polls

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

wp-graphql-logo-image

WPGraphQL Polls

This plugin extends the WPGraphQL & WP-Polls plugins and provide the interface for consuming the WP-Polls through queries and mutations.

This plugin is possible because of the work of Adrien Becuwe & 7aduta. Please, don't forget to checkout their respective work.

Wp-Polls Rest Api by Adrien Becuwe

Wp-Polls Rest Api by 7aduta

Install, Activate & Setup

You can install and activate the plugin like any WordPress plugin. Download the .zip from Github and add to your plugins directory, then activate.

It requires you to have WP-Polls plugin first in order to work. You can get it here.

Voting

This plugin adds a new vote mutation to the WPGraphQL Schema.

This can be used like so:

mutation PollVote {
  vote(input: {
    clientMutationId: "Vote",
    id: 1,
    userId: 1,
    answers: "1,2,3"
  }) {
    status
    message
  }
}
  • The integer id is unique identifier for the poll you are voting.
  • The integer userId is the identifier of the voter.
  • The string answers are the ids of the user selected answers. Note: they have to be comma separated only.

It returns a status code and a respective message after the operation has been executed or not.

Querying

You can query polls by doing the following query:

query GetAllPolls {
  polls {
    id
    question
    totalVotes
    open
    maxAnswers
    voted
    answers {
      id
      description
      votes
    }
  }
}
  • id: (integer) is the unique identifier for the poll you are voting.
  • question: (string) is a text describing the poll.
  • totalVotes: (integer) is the total amount of votes in this poll.
  • open: (boolean) is the current status of the poll. Telling either if it's available for voting or not.
  • maxAnswers: (integer) is the number of answers that can be selected per user before voting.
  • voted: (boolean) tells if the user has already voted in this poll.
  • answers: (array) is an array of object containing detailed information about the poll's options.
    • id: (integer) is the unique identifier for the respective answer.
    • description: (string) is a text representing an option for voting.
    • votes: (integer) is the unique identifier for the poll you are voting.

If you need to query an specific poll you can do it by passing the poll id through the query, like the example bellow:

query GetPollById {
  polls(id: 1) {
    ...
  }
}

Status

The statuses returned by the vote mutation are normal HTTP codes. The ones used in this plugin are:

  • 201: Created
  • 400: Bad Request
  • 401: Unauthorized
  • 403: Forbidden
  • 409: Conflict
  • 500: Internal Server Error

Thanks

I'd like to say many thanks to WPGraphQL, Lester Chan, Adrien Becuwe & 7aduta for their work.