GetGQLSchema queries for the GraphQL schema node, and returns the uid and the GraphQL schema. If multiple schema nodes were found, it returns an error.
(namespace uint64)
| 112 | // GetGQLSchema queries for the GraphQL schema node, and returns the uid and the GraphQL schema. |
| 113 | // If multiple schema nodes were found, it returns an error. |
| 114 | func GetGQLSchema(namespace uint64) (uid, graphQLSchema string, err error) { |
| 115 | ctx := context.WithValue(context.Background(), Authorize, false) |
| 116 | ctx = x.AttachNamespace(ctx, namespace) |
| 117 | resp, err := (&Server{}).QueryNoGrpc(ctx, |
| 118 | &api.Request{ |
| 119 | Query: ` |
| 120 | query { |
| 121 | ExistingGQLSchema(func: has(dgraph.graphql.schema)) { |
| 122 | uid |
| 123 | dgraph.graphql.schema |
| 124 | } |
| 125 | }`}) |
| 126 | if err != nil { |
| 127 | return "", "", err |
| 128 | } |
| 129 | |
| 130 | var result existingGQLSchemaQryResp |
| 131 | if err := json.Unmarshal(resp.GetJson(), &result); err != nil { |
| 132 | return "", "", errors.Wrap(err, "Couldn't unmarshal response from Dgraph query") |
| 133 | } |
| 134 | res := result.ExistingGQLSchema |
| 135 | if len(res) == 0 { |
| 136 | // no schema has been stored yet in Dgraph |
| 137 | return "", "", nil |
| 138 | } else if len(res) == 1 { |
| 139 | // we found an existing GraphQL schema |
| 140 | gqlSchemaNode := res[0] |
| 141 | return gqlSchemaNode.Uid, gqlSchemaNode.Schema, nil |
| 142 | } |
| 143 | |
| 144 | // found multiple GraphQL schema nodes, this should never happen |
| 145 | // returning the schema node which is added last |
| 146 | for i := range res { |
| 147 | iUid, err := dql.ParseUid(res[i].Uid) |
| 148 | if err != nil { |
| 149 | return "", "", err |
| 150 | } |
| 151 | res[i].UidInt = iUid |
| 152 | } |
| 153 | |
| 154 | sort.Slice(res, func(i, j int) bool { |
| 155 | return res[i].UidInt < res[j].UidInt |
| 156 | }) |
| 157 | glog.Errorf("namespace: %d. Multiple schema nodes found, using the last one", namespace) |
| 158 | resLast := res[len(res)-1] |
| 159 | return resLast.Uid, resLast.Schema, nil |
| 160 | } |
| 161 | |
| 162 | // UpdateGQLSchema updates the GraphQL and Dgraph schemas using the given inputs. |
| 163 | // It first validates and parses the dgraphSchema given in input. If that fails, |
no test coverage detected