Skip to main content

GraphQL is the many-to-many solution

Exactly! Regular readers of this blog (me) will appreciate my stumbling attempts to pre-define a REST interface that supports many-to-many interfaces. GET a class, for example, and the return includes an array of the students in that class. In this context, we don't want a full Student record, just the Student's name and Id, for example.

With a REST interface, the server writer has to guess how to abbreviate the Student record. GraphQL fixes that. The front end requests just the data it wants. If we want a list of the students in a class and the assigned roommates for that student...we can do that!

A lot of my prototype REST service is hardwired--not single tables, so much, but the many-to-many stuff certainly. There was a certain amount of work implementing the simple router ("/table/recordno").

GraphQL means throwing a lot of that away, but I can see immediately that GraphQL's approach is what I want. My schema tables (implementing INSERT and UPDATE) look a lot like GraphQL:

type Query {
    hero: Character
}

type Character {
    name: String
    friends: [Character]
    homeWorld: Planet
    species: Species
}

type Planet {
    name: String
    climate: String
}

type Species {
    name: String
    lifespan: Int
    origin: Planet
}


I don't know where GraphQL is "at" yet, but maybe I could contribute. There's graphql-php, and a library called graphql-relay-php that supports react-relay (whatever that is). Siler is a PHP library powered with high-level sbstractions to work with GraphQL.

Getting Started

So, I downloaded graphql-php from github. Github doesn't like my old password, and 1Password doesn't like Chrome for some reason, so that took a while to sort. The graphql-php library is documented in Markdown, so I got a Markdown viewer. Eh. Turns out, the library instructions start with composer, so I had to download that.

Composer is a php dependency manager, not a package manager like npm, but sort of. Installing Composer put php on my system PATH, so that's something.

Comments

Popular posts from this blog

JSON/MySQL Schemas

As noted previously, there is a lot of overlap between the RDBMS world and the JSON world. Identifiers JSON is defined to allow identifiers of any kind of Unicode string, encoded in UTF-8 (or UTF-16, etc.). They begin and end with double quotation marks (U+0022), so included quotation marks and '\' must be escaped as \" or \\. Control characters (ASCII 0-x1F) must be escaped as well. In practice, JSON identifiers conform to ECMAScript standards . There are some 68  reserved keywords (function, import, for, if, else, and so on) that should not be used as identifiers. Unexpected reserved words include abstract, await, debugger, delete, finally, instanceof, super, synchronized, transient, volatile, and yield. The spec makes a distinction between identifiers and IdentifierNames (specifically, array keys), but why risk it? ECMAScript allows '$' and '_' anywhere in an identifier. Length or camelCasing are not part of the spec. As for length, there seems t...

Why table join doesn't work

Everything in the traditional relational database world is based on tables. If I have a table of Classes and a table of Students, I can have an association table that tells me who's in which class. classId className instructor 1 Psych 101 Dr. Jung studentId firstName lastName 1 Molly Ringwald 2 Anthony Hall 3 Joan Cusack Clearly, these two tables tell me nothing about which classes Molly is taking, nor which students have signed up for Psych 101. An "association" table provides that information: classId studentId Association-specific notes 1 1 1 2 late add 1 3 In the relational world, a LEFT JOIN gives us the student list for a given class: className instructor firstName lastName Assn notes Psych 101 Dr. Jung Molly Ringwald Psych 101 Dr. Jung Anthony Hall late add Psych 101 Dr. Jung Joan Cusack This approach is "pure" from a Chris Date perspective, but when we're talking about JSON data in response to a RESTful HTTP r...

Relational Updates

The pattern I've been focused on for some time is the classic 'students in classes' or 'passengers on flights' problem. The database has a table of students and a table of classes and an association table which joins them. That's basic stuff. The problem is how do you update a student to edit her list of classes, or update a class to edit its list of students? The relational integrity is not a problem--if you delete a class from the student, that student no longer appears in the class' list of students. The problem is your simple form gets very complicated. Instead of a form that displays the columns of a single row in a table, now we have three tables involved. Of course it's solvable, but we want it to be intuitive for the end user. Old time software punted the problem--you could edit the association table, or a class, or a student, but you couldn't add classes to a student. What we want is a list of classes on the student record. Each class ...