How do I generate schema for subscriptions for rel...
# orm-help
v
How do I generate schema for subscriptions for relay?
get-graphql-schema
on the relay endpoint currently skips the subscription schema but it is returned for the simple API endpoint. I can't just copy paste the subscription schema sections into the relay schema because of missing types and general incompatibility between the two. What's a way to deal with this? I don't mind generating the missing subscription types/inputs via a script but I need help seeing what that should look like. Note: I'm OK switching to prisma at some point but can't do it just yet. Note: I'm aware of the discussion in https://github.com/graphcool/graphcool-framework/issues/85 and https://www.graph.cool/forum/t/relay-modern-subscriptions/420/7 but neither of those provides any details on how to go about converting to a relay schema from simple.
@nikolasburk Your team's awesome [tutorial on relay subscriptions](https://www.howtographql.com/react-relay/7-subscriptions/) references a [manually tweaked schema](https://www.graphqlbin.com/hn-relay-full.graphql) to include the subscriptions. I have two questions: * Was that hand written or was there a script used? * That example schema does not include <TypeName>SubscriptionFilter and <TypeName>SubscriptionFilterNode input specs. Why is that? Aren't those needed to specify filters on the fields to use for subscriptions?
n
yes, this is hand written 🙂
v
thanks @nilan. Could you please also address the second part:
That example schema does not include <TypeName>SubscriptionFilter and <TypeName>SubscriptionFilterNode input specs. Why is that? Aren't those needed to specify filters on the fields to use for subscriptions?
n
I am not sure, but I would guess they were not needed in that context
yes, they are needed to specify filters
v
For the benefit of anyone else trying to generate relay subscription schemas from simple ones, here are two scripts I just wrote:
Copy code
#!/bin/bash

export RELAY_IN_FILE="./data/schema.relay.graphql"
export SIMPLE_IN_FILE="./data/schema.simple.graphql"
export TYPES_OUT_FILE="./data/schema.subscription.lower.graphql"
export FINAL_SCHEMA_FILE="./data/schema.graphql"

export GEN_TYPES="./scripts/genLowerRelayTypesFromSimple"
export GET_SCHEMA="get-graphql-schema"

if [ -z ${SRI_APP_RELAY_URL+x} ]; then
    echo "ERROR: Must specify relay URL in variable \$SRI_APP_RELAY_URL"
    exit 1
fi

# Download latest relay schema
${GET_SCHEMA} $SRI_APP_RELAY_URL > ${RELAY_IN_FILE}

if [ "$?" -ne "0" ]; then
    echo "ERROR: Could not download latest relay schema from: " $SRI_APP_RELAY_URL
    exit 1
fi

# construct matching URL but for the simple API instead
export SRI_APP_SIMPLE_URL=`echo ${SRI_APP_RELAY_URL} | sed s/relay/simple/`

# Download latest simple schema
${GET_SCHEMA} ${SRI_APP_SIMPLE_URL} > ${SIMPLE_IN_FILE}

if [ "$?" -ne "0" ]; then
    echo "ERROR: Could not download latest simple schema from: " $SRI_APP_SIMPLE_URL
    exit 1
fi

${GEN_TYPES} ${SIMPLE_IN_FILE} > ${TYPES_OUT_FILE}

if [ "$?" -ne "0" ]; then
    echo "ERROR: Could not parse the simple schema lower types."
    exit 1
fi

# combine subscription types with relay schema
cat ${RELAY_IN_FILE} ${TYPES_OUT_FILE} > ${FINAL_SCHEMA_FILE}
The
genLowerRelayTypesFromSimple
file referred to above is:
Copy code
#!/usr/bin/awk -f

BEGIN { RS="}";} \
/^\n\ntype.*PreviousValues |^\n\n(type|input).*Subscription(Filter((Node)*)|Payload)* / \
{print $0RS} \
END { \
print "\n\nenum _ModelMutationType {"; \
print "  CREATED"; \
print "  UPDATED"; \
print "  DELETED"; \
print "}"; \
}
These are tested on a mac os x with the standard
awk
that ships with Darwin. YMMV.
🙌 1
n
thanks for sharing 😊