GraphQL schema problem trying to Query an object

Hi guys, Need some hopefully quick help with getting a schema setup. I have a collection of addresses with geocoded gps coordinates as an object.

type Office_Locations {
  Office_ID: String!
  Address: String!
  City: String!
  Province: String!
  geocodelocation: Location
  geocodetimezone: String
  Geocoded: Boolean
}

type Location {
  lat: Float
  lng: Float
}

type Query {
  Geocoded(Geocoded: Boolean): [Office_Locations!]
}

I am succesfully able to query the geocodetimezone like this:

query {
  Geocoded(Geocoded: true) {
    data {
      Address
      geocodetimezone
    }
  }
}

But the problem is when I try to query the coordinates like this

query {
  Geocoded(Geocoded: true) {
    data {
      Address
      geocodetimezone
	  geocodelocation {
        lat
        lng
      }
    }
  }
}

I get an error from Fauna

{
  "errors": [
    {
      "message": "Ref or Set expected, Object provided.",
      "extensions": {
        "code": "invalid argument"
      }
    }
  ]
}

It looks like it’s a problem with the schema and not understanding the type Location properly. Can someone help me explain how define the schema so it’s an object with lat and lng and how to query that back? I’m assuming this is super simple but just a learning curve problem in knowing how to define the schema to include inner objects properly. Thanks in advance for your help!

Here’s what one of the Documents looks like for your reference:

{
  Province: "ON",
  Office_ID: "10000",
  Address: "123 Main Street",
  City: "Pickering",
  geocodelocation: {
    lat: 43.13851,
    lng: -79.088272
  },
  geocodetimezone: "America/Toronto",
  Geocoded: true
}

Hi @Hal

Your schema has one-to-one relation between Office_Locations and Location which means the data geocodelocation should have a reference to Location document. But in the example you gave location is embedded.

Two ways to resolve this.

  1. Update your schema to use @embedded directive i.e. modify type Location to type Location @embedded
  2. Update your Office_Location document to have Ref of Location instead of object.

How did you create the Office_Locations and Location documents ? My guess is you used Create using FQL and GraphQL API is not able to translate because for the relation it was expecting a ref or set but Object was provided.

More details on what happens behind the scenes with your schema

  1. Each type creates a Collection

    Paginate(Collections());{
      data: [Collection("Office_Locations"), 
             Collection("Location")]
    }
    
  2. Each type Query creates an Index

    Paginate(Indexes());
    {
       data: [Index("Geocoded")]
    }
    
  3. One to One relation does not create any additional Index but stores the reference of other document.

Created an Office_Locations and Location documents through GraphQL API.

Query to Fetch documents with Geocoded: true

How is data stored in database ?

Office_Locations

Map(Paginate(Documents(Collection("Office_Locations"))), Lambda("x", Get(Var("x"))))
{
 data: [
   {
     ref: Ref(Collection("Office_Locations"), "278646283541938694"),
     ts: 1601996654648000,
     data: {
       geocodetimezone: "America/Toronto",
       Province: "ON",
       geocodelocation: Ref(Collection("Location"), "278646283460149766"),
       Address: "123 Main Street",
       Office_ID: "10000",
       Geocoded: true,
       City: "Pickering"
     }
   }
 ]
}

Location

Map(Paginate(Documents(Collection("Location"))), Lambda("x", Get(Var("x"))))

{
  data: [
    {
      ref: Ref(Collection("Location"), "278646283460149766"),
      ts: 1601996654648000,
      data: {
        lat: 43.13851,
        lng: -79.088272
      }
    }
  ]
}

Hope this helps.

1 Like

Wow. Thank you for the detail. This solved my issue while giving me a deeper understanding.