Skip to content

Instantly share code, notes, and snippets.

@gc-codesnippets
Created November 19, 2018 22:01
Show Gist options
  • Select an option

  • Save gc-codesnippets/b7c0d5aae4541f6650c968477259b611 to your computer and use it in GitHub Desktop.

Select an option

Save gc-codesnippets/b7c0d5aae4541f6650c968477259b611 to your computer and use it in GitHub Desktop.

Revisions

  1. gc-codesnippets created this gist Nov 19, 2018.
    69 changes: 69 additions & 0 deletions Untitled-2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    const query1 = `{
    user(id: "$id") {
    *
    }
    }`
    type query1 = `{
    user(id: "$id") {
    *
    }
    }`

    type Result1 = {
    user: {
    id: string
    name: string
    }
    }

    const query2 = `{
    posts {

    }
    }`
    type query2 = `{
    posts {

    }
    }`

    type Result2 = {
    posts: {
    id: string
    title: string
    }[]
    }

    function $graphql(q: query1, args?: any): Result1
    function $graphql(q: query2, args?: any): Result2
    function $graphql(q: string, args?: any): any
    function $graphql(
    q: query1 | query2 | string,
    args?: any,
    ): Result1 | Result2 | any {
    switch (q) {
    case query1:
    return <Result1>{ user: { id: 'xxx', name: 'xxx' } }
    case query2:
    return <Result2>{ posts: [{ id: 'xxx', title: 'xxx' }] }
    default:
    return ''
    }
    }

    const myRes1 = $graphql(
    `{
    user(id: "$id") {
    *
    }
    }`,
    { id: 'sfds' },
    )

    const myRes2 = $query(`{
    posts {

    }
    }`)

    const other = $graphql(`xxx`)