Skip to content

Instantly share code, notes, and snippets.

@nikolasburk
Last active March 7, 2020 17:50
Show Gist options
  • Select an option

  • Save nikolasburk/7dbe73758d68d09001ff3c1da8f1826f to your computer and use it in GitHub Desktop.

Select an option

Save nikolasburk/7dbe73758d68d09001ff3c1da8f1826f to your computer and use it in GitHub Desktop.

Revisions

  1. nikolasburk revised this gist Jun 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion instructions.md
    Original file line number Diff line number Diff line change
    @@ -76,6 +76,6 @@ touch myapi/.graphqlconfig.yml
    graphql get-schema
    ```

    The GraphQL CLI reads the `schemaPath` property from `.graphqlconfig.yml` and downloads the schema via an introspection query from the API.
    The GraphQL CLI reads the `schemaPath` property from `.graphqlconfig.yml`, downloads the schema via an introspection query from the API and stores it in that path.


  2. nikolasburk revised this gist Jun 12, 2018. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion instructions.md
    Original file line number Diff line number Diff line change
    @@ -64,6 +64,10 @@ touch myapi/.graphqlconfig.yml
    graphql playground
    ```

    > **Pro tip**: To get the full experience, download and install the standalone Desktop version of the GraphQL Playground from [here](https://github.com/prismagraphql/graphql-playground/releases).

    ![](https://imgur.com/XVsVbBq.png)

    ## 5. (Optional) Download the GraphQL schema of the Contentful GraphQL API

    Run the following command in your terminal:
    @@ -72,4 +76,6 @@ touch myapi/.graphqlconfig.yml
    graphql get-schema
    ```

    The GraphQL CLI reads the `schemaPath` property from `.graphqlconfig.yml` and downloads the schema via an introspection query from the API.
    The GraphQL CLI reads the `schemaPath` property from `.graphqlconfig.yml` and downloads the schema via an introspection query from the API.


  3. nikolasburk created this gist Jun 12, 2018.
    75 changes: 75 additions & 0 deletions instructions.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    # Use the Contentful GraphQL API inside a GraphQL Playground

    The Contentful GraphQL API ships with GraphiQL by default. To get even better workflows (such as multiple tabs, speciyfing HTTP headers or work with multiple GraphQL APIs side-by-side), you can use a [GraphQL Playground](https://github.com/prismagraphql/graphql-playground).

    ## 1. Get the endpoint for your Contentful GraphQL API

    The endpoint of your Contentful GraphQL API has the following structure: `https://cdn.contentful.com/spaces/{SPACE}/graphql/alpha/explore?access_token={CDA_TOKEN}`

    There are two placeholders that need to be replaced:

    - `SPACE`: The ID of your Contentful space, e.g. `9t7hdjj3yos9`
    - `CDA_TOKEN`: The access token for the Content Delivery API of your Contentful space, e.g. `9a6ce8c14bbf5777c01e7c39a67d9bea3b491af2eb8647ca08c9ec26ba8a23b1`

    Opening this endpoint in a browser gives you access to GraphiQL.

    ## 2. Install the GraphQL CLI

    ```bash
    npm install -g graphql-cli
    ```

    ## 3. Create a `.graphqlconfig`

    The `graphql playground` command of the GraphQL CLI reads information from a local `.graphqlconfig` (which follows the `graphql-config` standard).

    ```bash
    mkdir myapi
    touch myapi/.graphqlconfig.yml
    ```

    Now add the following contents to the newly created `.graphqlconfig.yml`:

    ```yml
    projects:
    cf:
    schemaPath: contentful.graphql
    extensions:
    endpoints:
    default:
    url: https://cdn.contentful.com/spaces/{SPACE}/graphql/alpha?locale=en-US
    headers:
    Authorization: Bearer {CDA_TOKEN}
    ```
    Replace `{SPACE}` and `{CDA_TOKEN}` with the values for your space ID and the access token for the Content Delivery API (as before). Here is what the result could look like:

    ```yml
    projects:
    cf:
    schemaPath: contentful.graphql
    extensions:
    endpoints:
    default:
    url: https://cdn.contentful.com/spaces/9t7hdij3yor9/graphql/alpha?locale=en-US
    headers:
    Authorization: Bearer 9a6ce8c14abf5777c01e7c39a67d9bea3b491af2eb8647ca08c9eb26ba8a23b1
    ```

    ## 4. Open a GraphQL Playground

    Run the following command in your terminal:

    ```bash
    graphql playground
    ```

    ## 5. (Optional) Download the GraphQL schema of the Contentful GraphQL API

    Run the following command in your terminal:

    ```bash
    graphql get-schema
    ```

    The GraphQL CLI reads the `schemaPath` property from `.graphqlconfig.yml` and downloads the schema via an introspection query from the API.