Google Fonts API & Apollo


#1

So I thought I’d try setting up an Apollo query to allow me to get a list of Google Fonts from their REST API, and it all seems to make sense, except I’m not sure what to do about the FontFiles object, as it seems that numbers aren’t allowed in the field names… But I want to expose the fields as they are in the API so as to make it 100% the same…

Currently I get the following error:

{
  "errors": [
    {
      "message": "Syntax Error GraphQL (154:3) Expected Name, found Int \"100\"\n\n153: type FontFiles {\n154:   100: String\n       ^\n155:   100italic: String\n",
      "locations": [
        {
          "line": 154,
          "column": 3
        }
      ]
    }
  ]
}

And here is my Schema definition and resolvers:

import { HTTP } from 'meteor/http';
import { Random } from 'meteor/random';

export const schema = [`
type FontFiles {
  100: String
  100italic: String
  200: String
  200italic: String
  300: String
  300italic: String
  regular: String
  italic: String
  500: String
  500italic: String
  600: String
  600italic: String
  700: String
  700italic: String
  800: String
  800italic: String
  900: String
  900italic: String
}

type Font {
  family: String!
  variants: [String]
  files: FontFiles
}

type RootQuery {
  googleFonts: [Font]
}

schema {
  query: RootQuery
}
`];

export const resolvers = {
  RootQuery: {
    async googleFonts() {
      return new Promise((fulfill, reject) => {
        HTTP.get(
          'https://www.googleapis.com/webfonts/v1/webfonts?fields=items(family%2Cfiles%2Cvariants)&sort=popularity&key=AIzaSyBY602OfhH3YPNKEcXdnwc8NxZC2IrzyiQ',
          (error, result) => {
            if (error) reject(error);
            fulfill(result.data.items);
          }
        );
      });
    },
  },
  Font: {
    files: ({ files }) => files,
  },
};