Apollo with Raw Sequelize Queries/GeoJSON


#1

I’m building my first Apollo app using US Census data, and I’m running into issues designing my schema and resolvers. Currently, I have one resolver (a Sequelize raw query) which returns a GeoJSON FeatureCollection from Postgres. However, I’m getting confused on how to design the schema. I’m trying to replicate the GeoJSON spec in my schema but keep receiving an Resolve function missing... error when trying to connect to GraphiQL. Below is my schema. Any ideas on how to get this working? Thanks.

const typeDefinitions = `
  type Query {
    census: FeatureCollection
  }

  schema {
    query: Query
  }

  type CensusData {
    display_name: String,
    aland: Int,
    awater: Int,
    geoid: String,
    pop_35_54: Float,
    pop_55_over: Float,
    pop65_over: Float,
    hh_with_children: Float,
    single: Float,
    never_married: Float,
    separated: Float,
    other_single: Float,
    widowed: Float,
    divorced: Float,
    median_income: Float,
    pop_50k_65_over: Float,
    rent_1000_over: Float
  }

  type Geom {
    type: String,
    coordinates: [Float]
  }

  type Feature {
    type: String,
    geometry: Geom,
    properties: CensusData
  }

  type FeatureCollection {
    type: String,
    features: [Feature]
  }
`;

export default [typeDefinitions];

#2

Did you pas any resolvers to apolloServer? A quick way to test if your schema works, is to pass no resolvers and pass mocks: {}. After that, you need a resolve function for each non-scalar field, and each field with arguments. For example you need one for Query.census:

const resolvers = {
  Query: {
    census(){
      return {}; // return the feature collection here.
    }
  }
};

Once you’ve defined one for all the non-scalar fields (i.e. all fields that are not Int, String or something like that), you should be good to go.

If your resolver doesn’t need to do any work, you can just make it like this: stuff: (thing) => thing.stuff


#3

Thanks for the response. I missed the point about every non-scalar field needing a resolver, so that makes sense why I was getting an error.

Right now, I have a single resolver with a really complex SQL query which returns a GeoJSON feature collection which I was trying to map to my schema. For now, I scaled down my schema to a single string and am returning the query as a string which I parse into Leaflet on the client. It’s working well for my purposes but still seems a little gross.

The confusing thing to be about creating resolvers for my schema above is that some of the fields (like type: String) are simply text strings, not queries to the db or anything. How do you handle that situation as well as nested objects? Am I just thinking about this the wrong way?


#4

For the fields that are just strings or ints, you don’t have to create a resolve function. I’m refactoring Apollo Server right now, and I’ll try to make the documentation for it very clear.