Skip to content

Instantly share code, notes, and snippets.

@davist11
Last active January 22, 2022 14:35
Show Gist options
  • Select an option

  • Save davist11/129038286f04bc5e2941baae32ee509e to your computer and use it in GitHub Desktop.

Select an option

Save davist11/129038286f04bc5e2941baae32ee509e to your computer and use it in GitHub Desktop.

Revisions

  1. davist11 revised this gist Jan 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion graphql.server.jsx
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ export const gqlClient = (request = null) => {

    return new GraphQLClient(`https://your-endpoint-here/${queryString}`, {
    headers: {
    authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`,
    authorization: `Bearer YOUR_AUTH_TOKEN_HERE`,
    },
    })
    }
  2. davist11 revised this gist Jan 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion graphql.server.jsx
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import { GraphQLClient } from 'graphql-request'

    // Pass through allowed query params to the requst
    const getQueryParams = (request): string => {
    const getQueryParams = (request) => {
    const url = new URL(request.url)
    const allowedKeys = ['x-craft-preview', 'x-craft-live-preview', 'token']
    const filteredParams = Object.entries(
  3. davist11 revised this gist Jan 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion $slug.jsx
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import { json } from 'remix'
    import { gql } from 'graphql-request'
    import { gqlClient } from '~/helpers/graphql.server'
    import { gqlClient } from 'graphql.server'

    export const loader = async ({ request }) => {
    const { entries } = await gqlClient(request).request(gql`
  4. davist11 revised this gist Jan 22, 2022. 2 changed files with 29 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion $slug.jsx
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ import { gqlClient } from '~/helpers/graphql.server'
    export const loader = async ({ request }) => {
    const { entries } = await gqlClient(request).request(gql`
    {
    GRAPHQL_REQUEST_HERE
    YOUR GRAPHQL QUERY HERE
    }
    `)

    28 changes: 28 additions & 0 deletions graphql.server.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import { GraphQLClient } from 'graphql-request'

    // Pass through allowed query params to the requst
    const getQueryParams = (request): string => {
    const url = new URL(request.url)
    const allowedKeys = ['x-craft-preview', 'x-craft-live-preview', 'token']
    const filteredParams = Object.entries(
    Object.fromEntries(url.searchParams)
    ).filter(([key]) => allowedKeys.includes(key))

    if (!filteredParams.length) {
    return ''
    }

    const queryString = filteredParams.map((val) => val.join('=')).join('&')

    return `?${queryString}`
    }

    export const gqlClient = (request = null) => {
    const queryString = request ? getQueryParams(request) : ''

    return new GraphQLClient(`https://your-endpoint-here/${queryString}`, {
    headers: {
    authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`,
    },
    })
    }
  5. davist11 revised this gist Jan 22, 2022. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions $slug.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    import { json } from 'remix'
    import { gql } from 'graphql-request'
    import { gqlClient } from '~/helpers/graphql.server'

    export const loader = async ({ request }) => {
    const { entries } = await gqlClient(request).request(gql`
    {
    GRAPHQL_REQUEST_HERE
    }
    `)

    return json({ entries })
    }
  6. davist11 revised this gist Jan 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion general.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    return [
    '*' => [
    '*' => [
    'headlessMode' => true,
    'previewIframeResizerOptions' => [
    'checkOrigin' => [
  7. davist11 created this gist Jan 22, 2022.
    12 changes: 12 additions & 0 deletions general.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <?php

    return [
    '*' => [
    'headlessMode' => true,
    'previewIframeResizerOptions' => [
    'checkOrigin' => [
    'https://www.trevor-davis.com',
    ],
    ],
    ],
    ];