<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Erik van Eykelen</title>
    <description>The latest articles on DEV Community by Erik van Eykelen (@evaneykelen).</description>
    <link>https://dev.to/evaneykelen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F103326%2Ff605ac43-ba7b-4e32-83e3-1abad6655d4b.jpeg</url>
      <title>DEV Community: Erik van Eykelen</title>
      <link>https://dev.to/evaneykelen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/evaneykelen"/>
    <language>en</language>
    <item>
      <title>Using GraphQL with the WIP API</title>
      <dc:creator>Erik van Eykelen</dc:creator>
      <pubDate>Sat, 15 Jun 2019 15:51:32 +0000</pubDate>
      <link>https://dev.to/evaneykelen/using-graphql-with-the-wip-api-2cb1</link>
      <guid>https://dev.to/evaneykelen/using-graphql-with-the-wip-api-2cb1</guid>
      <description>&lt;p&gt;WIP (&lt;a href="https://wip.chat/"&gt;https://wip.chat/&lt;/a&gt;) is a community of makers started by &lt;a href="https://wip.chat/@marckohlbrugge/"&gt;Marc Köhlbrugge&lt;/a&gt;. Recently I dug into its GraphQL API to check out the capabilities. Below you'll find my notes.&lt;/p&gt;

&lt;p&gt;After signing up you can go &lt;a href="https://wip.chat/api"&gt;this page&lt;/a&gt; which displays your private API key.&lt;/p&gt;

&lt;p&gt;Let's start by requesting your own account profile.&lt;/p&gt;

&lt;p&gt;Two headers must be set:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Authorization: bearer YOUR-PRIVATE-API-KEY&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Content-Type: application/json&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The API endpoint is &lt;a href="https://wip.chat/graphql"&gt;https://wip.chat/graphql&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Send the following query to obtain your account profile data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  viewer {
    id
    url
    username
    first_name
    last_name
    avatar_url
    best_streak
    completed_todos_count
    products {
      name
      todos {
        body
      }
    }
    todos {
      body
    }
    streaking
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the use of &lt;code&gt;viewer&lt;/code&gt;. You'll see this often in GraphQL APIs. It refers to the authenticated user.  It's not an actual GraphQL specification but rather a &lt;a href="https://medium.com/the-graphqlhub/graphql-and-authentication-b73aed34bbeb"&gt;practice&lt;/a&gt; which originated at Facebook (just as GraphQL).&lt;/p&gt;

&lt;p&gt;The WIP API uses &lt;code&gt;viewer&lt;/code&gt; to return data associated to the authenticated and authorized API user.&lt;/p&gt;

&lt;p&gt;The result looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "data": {
    "viewer": {
      "id": "1377",
      "url": "https://wip.chat/@realhackteck",
      "username": "realhackteck",
      "first_name": "Erik",
      "last_name": "van Eykelen",
      "avatar_url": "https://wip.imgix.net/store/user/1377/avatar/5c9fae4c0cd2343c8a86edbf822825b4.jpg?ixlib=rb-1.2.2&amp;amp;w=64&amp;amp;h=64&amp;amp;fit=crop&amp;amp;s=6c4e9eb0dd3b1679c34b056ad532d05b",
      "best_streak": 0,
      "completed_todos_count": 0,
      "products": [
        {
          "name": "Wip.Chat API test",
          "todos": []
        }
      ],
      "todos": [],
      "streaking": false
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: replacing &lt;code&gt;viewer&lt;/code&gt; by &lt;code&gt;user(username: "realhackteck")&lt;/code&gt; will return the same data.&lt;/p&gt;

&lt;p&gt;One of the cool things about GraphQL is its ability to provide introspection:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zhEc-hbu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/11846/59552940-b321ac80-8f8d-11e9-9373-fe70b70bc609.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zhEc-hbu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/11846/59552940-b321ac80-8f8d-11e9-9373-fe70b70bc609.png" alt="Insomnia"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The screenshot shows the &lt;a href="https://insomnia.rest/"&gt;Insomnia&lt;/a&gt; client which I use a lot to run tests against APIs. The popup with the orange-colored Gs shows attributes which belong to the &lt;code&gt;Todo&lt;/code&gt; object. Insomnia is able to display these attributes by querying the GraphQL schema. &lt;a href="https://graphql.org/learn/introspection/"&gt;This article&lt;/a&gt; explains how introspection works.&lt;/p&gt;

&lt;p&gt;The next step is to create something using the API. The following example shows how you can create a WIP todo including an attachment. Three steps are needed to accomplish this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;Generate a pre-signed URL which you can use to upload an attachment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation createPresignedUrl {
  createPresignedUrl(input: {filename: "foobar.jpg"}) {
    fields
    headers
    method
    url
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "data": {
    "createPresignedUrl": {
      "fields": "{\"key\":\"cache/c79b...7fe0.jpg\",\"Content-Type\":\"image/jpeg\",\"policy\":\"eyJl...fQ==\",\"x-amz-credential\":\"AKIA...HQKQ/20190615/us-east-1/s3/aws4_request\",\"x-amz-algorithm\":\"AWS4-HMAC-SHA256\",\"x-amz-date\":\"20190615T150217Z\",\"x-amz-signature\":\"497b...13df\"}",
      "headers": "{}",
      "method": "post",
      "url": "https://s3.amazonaws.com/assets.wip.chat"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;p&gt;Use the previous result to upload the attachment to AWS S3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -F "key=cache/c79b...7fe0.jpg" \
     -F "Content-Type=image/jpeg" \
     -F "Policy=eyJl...fQ==" \
     -F "x-amz-credential=AKIA...HQKQ/20190601/us-east-1/s3/aws4_request" \
     -F "x-amz-algorithm=AWS4-HMAC-SHA256" \
     -F "x-amz-date=20190615T150217Z" \
     -F "x-amz-signature=497b...13df" \
     -F "file=@foobar.jpg" \
     https://s3.amazonaws.com/assets.wip.chat
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;p&gt;Send a &lt;a href="https://graphql.org/learn/queries/"&gt;mutation&lt;/a&gt; request to the GraphQL API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation createTodo {
  createTodo(input: {body: "This todo has an attachment", attachments: [{key: "cache/c79b...7fe0.jpg", filename: "foobar.jpg", size: 1001}]}) {
    id
    body
    user {
      first_name
      last_name
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "data": {
    "createTodo": {
      "id": "117462",
      "body": "This todo has an attachment",
      "user": {
        "first_name": "Erik",
        "last_name": "van Eykelen"
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Check out &lt;a href="https://wip.chat/graphiql"&gt;https://wip.chat/graphiql&lt;/a&gt; if you're interested to know what you can do with the WIP API. This page runs GraphiQL which is a graphical in-browser IDE. Check out &lt;a href="https://github.com/graphql/graphiql"&gt;https://github.com/graphql/graphiql&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;This was originally published at &lt;a href="https://www.msgtrail.com/articles/20190615-1735-using-graphql-with-the-wip-api/"&gt;https://www.msgtrail.com/articles/20190615-1735-using-graphql-with-the-wip-api/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>wip</category>
    </item>
  </channel>
</rss>
