Getting issue in fetching all legislators by state


This post was flagged by the community and is temporarily hidden.


Using graphiql.

If you aren’t already, I strongly recommend you use graphiql or similar intelligent tools to sandbox your queries. They will redline any portions of the query that are syntactically wrong or don’t comply with the schema. Here is the graphiql sandbox for OpenStates. Here’s the best graphiql tutorial I can find. In graphiql, note especially the generated schema doc on the right side; it’s great for planning out queries. (Press the <Docs button at top right of graphiql to open the schema doc.)

Getting the right legislators the wrong way.

Ok, to start, you need to restrict the legislators you’re returning. Here’s one way.

Query 1: A wrong way to get legislators.

In this query, you may find classification: ["lower", "upper", "legislature"] a little puzzling - what’s legislature for? The answer is Nebraska and Washington D.C. Because their legislatures only have a single chamber it’s not “upper” or “lower”.

This query works sometimes. The problem with this is that if you request a lot of fields for each legislator, and there are a lot of legislators, the response will be too big. See this post for my highly imperfect understanding of current limits.

So, a harder but better way is to use a people() query, so that you can limit how many you get back.

Using a people() query.

In the graphiql documentation explorer, clicking on the top-level “Query” link shows you the top-level queries that are available, along with their parameters. The schema for the people() query reads:

people( memberOf: String everMemberOf: String district: String name: String updatedSince: String latitude: Float longitude: Float before: String after: String first: Int last: Int ): PersonConnection

Clearly, the only query parameter that will restrict the returned people to the state you want is memberOf. So the first thing we need to do is to get the organization ids of the chambers of the state. For that, use something like:

Query 2: Getting chamber ids.

and pull the ids out. Then use each in a people query, like this one for the Colorado Senate:

Query 3: Getting the first 5 legislators.

Ok, that gets you the first 5 legislators in the Colorado. We could have asked for up to 100. Unfortunately, some states have more than 100 legislators in a chamber, and the maximum you can set the “first” parameter to is 100. Also, the response might be too big if you ask for a lot of data on each legislator. So you need to paginate the results.

To get pages after the first one, you need to supply an after parameter providing the cursor of the last legislator retrieved on the previous page. In Query 3, we retrieved that in

"pageInfo": {
        "hasNextPage": true,
        "endCursor": "YXJyYXljb25uZWN0aW9uOjQ="

So for the second page of 5 Colorado legislators, you’d use

people(memberOf: "ocd-organization/98d43d46-9571-4a40-9007-581d84d41bb8", first: 5, after: "YXJyYXljb25uZWN0aW9uOjQ=")

Try editing the last graphiql query by replacing the people() line with this one, and rerun it. You should see the next 5 senators.

Getting committee and party memberships.

I have not worked with committees before; there may be gotchas that I’m not aware of.

This part is pretty easy, especially if you use the graphiql doc and autocompletion to help guide you through constructing it there.

Query 4: Getting committee and party memberships.

The role field seems to be blank for party membership. Also, the startDate and endDate fields seem to be always empty.

Using query variables.

You can create parameterized queries that can accept external query variables, rather than using string concatenation or templating to reconstruct the entire query every time. This is simpler on the client, and allows the server to cache query strategies, lookahead, etc. For example:

Query 5: Paging with query variables for chamber and cursor.

graphql query variables are not passed as HTTP query variables. Instead, they are passed in the HTTP post body JSON, in a variables member adjacent to the query. For more, see graphql doc, especially the bottom of the page.

Do not include the leading “$” on the variable names in the variables json.


When pasting a graphiql URL into a Discourse link, do not use the Link tool in the editor; it often butchers the URL. Instead, type in the parentheses and brackets yourself.

The limit of 5 legislators per query in these examples was arbitrary; it’s not a recommendation. However, 100 would be too many for a large query like Query 5.