TypeDefinitionRegistry getTypeDefinitionRegistry()

in src/main/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutor.java [348:395]


    TypeDefinitionRegistry getTypeDefinitionRegistry(@NotNull String sdl, @NotNull Resource currentResource, @NotNull String[] selectors) {
        TypeDefinitionRegistry typeRegistry = null;
        readLock.lock();
        String newHash = SHA256Hasher.getHash(sdl);
        /*
        Since the SchemaProviders that generate the SDL can dynamically change, but also since the resource is passed to the RuntimeWiring,
        there's a two stage cache:

        1. a mapping between the resource, selectors and the SDL's hash
        2. a mapping between the hash and the compiled GraphQL schema
         */
        String resourceToHashMapKey = getCacheKey(currentResource, selectors);
        String oldHash = resourceToHashMap.get(resourceToHashMapKey);
        if (!newHash.equals(oldHash)) {
            readLock.unlock();
            writeLock.lock();
            try {
                oldHash = resourceToHashMap.get(resourceToHashMapKey);
                if (!newHash.equals(oldHash)) {
                    typeRegistry = new SchemaParser().parse(sdl);
                    typeRegistry.add(Directives.CONNECTION);
                    typeRegistry.add(Directives.FETCHER);
                    typeRegistry.add(Directives.RESOLVER);
                    for (ObjectTypeDefinition typeDefinition : typeRegistry.getTypes(ObjectTypeDefinition.class)) {
                        handleConnectionTypes(typeDefinition, typeRegistry);
                    }
                    resourceToHashMap.put(resourceToHashMapKey, newHash);
                    hashToSchemaMap.put(newHash, typeRegistry);
                }
            } catch (Exception e) {
                LOGGER.error("Unable to generate a TypeRegistry.", e);
            } finally {
                readLock.lock();
                writeLock.unlock();
            }
        }
        try {
            /*
             * when the cache is disabled we need to return the registry directly, since it will be created for each request
             */
            if (typeRegistry != null) {
                return typeRegistry;
            }
            return hashToSchemaMap.get(newHash);
        } finally {
            readLock.unlock();
        }
    }